<?php

class contact extends Controller {

	var $afterSendMail = '';
	
	function contact()
	{
		parent::Controller();
			
	}
	
		
	function index()
	{
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		
	     $data['contact_intro'] = $this->filemanagementmodel->getByPostion('contact_intro');
	    $data['contact_01'] = $this->filemanagementmodel->getByPostion('contact_01');
	    $data['contact_02'] = $this->filemanagementmodel->getByPostion('contact_02');
	    $data['contact_03'] = $this->filemanagementmodel->getByPostion('contact_03');
	    $data['contact_04'] = $this->filemanagementmodel->getByPostion('contact_04');
	    $data['contact_05'] = $this->filemanagementmodel->getByPostion('contact_05');
	    $data['contact_map_01'] = $this->filemanagementmodel->getByPostion('contact_map_01');
	    $data['contact_map_02'] = $this->filemanagementmodel->getByPostion('contact_map_02');
	    $data['contact_map_03'] = $this->filemanagementmodel->getByPostion('contact_map_03');
	    $data['contact_map_04'] = $this->filemanagementmodel->getByPostion('contact_map_04');
	    $data['contact_map_05'] = $this->filemanagementmodel->getByPostion('contact_map_05');
	    $data['contact_cont'] = $this->filemanagementmodel->getByPostion('contact_cont');
	    $this->load->plugin('captcha');
	    $capCode = array('capCode' => rand(0,9).rand(0,9).rand(0,9).rand(0,9));
	    $vals = array(
					'word'		 => $capCode['capCode'],
					'img_path'	 => './data-image/captcha/',
					'img_url'	 => site_url().'data-image/captcha/',
					'font_path'	 => './system/fonts/comic.ttf',
					'img_width'	 => '100',
					'img_height' => 30,
					'expiration' => 7200
				);
	
		$cap = create_captcha($vals);
		
		$dd = array(
					'captcha_id'	=> '',
					'captcha_time'	=> $cap['time'],
					'ip_address'	=> $this->input->ip_address(),
					'word'			=> $cap['word']
				);
		
		$query = $this->db->insert_string('captcha', $dd);
		$this->db->query($query);
		$data['cap'] = $cap;
		
	    if($this->afterSendMail != '' && $this->afterSendMail['success'] != '') {
	    	$data['success'] = $this->afterSendMail['success'];
	    }
		$this->load->view('f_header', $data);
		$this->load->view('f_contact', $data);
		$this->load->view('f_footer', $data);
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('contractmodel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$data['contract_id'] = $id[$i];
	    	$this->contractmodel->delete($data);
	    }	    
	    $this->listall();
	}
	
	function listall() 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('contractmodel');
	    $data['info'] =  $this->contractmodel->getAll();
	    $this->load->view('header');
		$this->load->view('contractus-list-form', $data);
		$this->load->view('footer');
	}
	
	function send()
	{
		$data['name'] = $this->input->post('txtName') ? $this->input->post('txtName') : '';
		$data['mail'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : 'tutorland_admin@tutorland.co.th';
		$data['tel'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
		$data['time'] = $this->input->post('txtTime') ? $this->input->post('txtTime') : null;
		$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		$data['capcode'] = $this->input->post('txtCap') ? $this->input->post('txtCap') : null;

		
		$expiration = time()-7200; // Two hour limit
		$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);		
	
		// Then see if a captcha exists:
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? ";
		$binds = array($data['capcode'], $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();
	
		if ($row->count == 0){
			// not mathc cap code
			$this->load->model('filemanagementmodel');
		    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
		    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
			
		    $data['contact_intro'] = $this->filemanagementmodel->getByPostion('contact_intro');
		    $data['contact_01'] = $this->filemanagementmodel->getByPostion('contact_01');
		    $data['contact_02'] = $this->filemanagementmodel->getByPostion('contact_02');
		    $data['contact_03'] = $this->filemanagementmodel->getByPostion('contact_03');
		    $data['contact_04'] = $this->filemanagementmodel->getByPostion('contact_04');
		    $data['contact_05'] = $this->filemanagementmodel->getByPostion('contact_05');
		    $data['contact_map_01'] = $this->filemanagementmodel->getByPostion('contact_map_01');
		    $data['contact_map_02'] = $this->filemanagementmodel->getByPostion('contact_map_02');
		    $data['contact_map_03'] = $this->filemanagementmodel->getByPostion('contact_map_03');
		    $data['contact_map_04'] = $this->filemanagementmodel->getByPostion('contact_map_04');
		    $data['contact_map_05'] = $this->filemanagementmodel->getByPostion('contact_map_05');
		    
		   $this->load->plugin('captcha');
		    $capCode = array('capCode' => rand(0,9).rand(0,9).rand(0,9).rand(0,9));
		    $vals = array(
						'word'		 => $capCode['capCode'],
						'img_path'	 => './data-image/captcha/',
						'img_url'	 => site_url().'data-image/captcha/',
						'font_path'	 => './system/fonts/comic.ttf',
						'img_width'	 => '100',
						'img_height' => 30,
						'expiration' => 7200
					);
		
			$cap = create_captcha($vals);
			
			$dd = array(
						'captcha_id'	=> '',
						'captcha_time'	=> $cap['time'],
						'ip_address'	=> $this->input->ip_address(),
						'word'			=> $cap['word']
					);
			
			$query = $this->db->insert_string('captcha', $dd);
			$this->db->query($query);
			$data['cap'] = $cap;
		
		    $data['success'] = "คุณใส่ Security Code ไม่ถูกต้อง กรุณากรอกอีกครั้งค่ะ";
			$this->load->view('f_header', $data);
			$this->load->view('f_contact', $data);
			$this->load->view('f_footer', $data);
			return;
		}
		
		//save data
		$this->load->model('contractmodel');
		$dataDB = array();
		$dataDB['name'] = $data['name'];
		$dataDB['email'] = $data['mail'];
		$dataDB['tel'] = $data['tel'];
		$dataDB['contract_time'] = $data['time'];
		$dataDB['note'] = $data['note'];
		$dataDB['from'] = 'C';
		$dataDB['is_active'] = '1';
		$this->contractmodel->add($dataDB);
		
		// send mail;
		$this->load->library('email');
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';
		$this->email->initialize($config);
		
		$this->email->from($data['mail'], $data['name']);
//		$this->email->to('pun.spider@gmail.com');
		$this->email->to('connect_bvk@hotmail.com');
		
		$this->email->subject('[C] กรุณาติดต่อกลับ คุณ ' . $data['name']);
		
		$msgMail = '
		<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		</head>
		
		<body>
		<p>กรุณาติดต่อกลับ คุณ' . $data['name']. '</p>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <td>อีเมล์</td>
		    <td>'. $data['mail'] .'</td>
		  </tr>
		  <tr>
		    <td>โทรศัพท์</td>
		    <td>'. $data['tel'] .'</td>
		  </tr>
		  <tr>
		    <td>เวลาที่สะดวกให้ติดต่อทางโทรศัพท์</td>
		    <td>'. $data['time'] .'</td>
		  </tr>
		  <tr>
		    <td>ข้อความ</td>
		    <td>'. $data['note'] .'</td>
		  </tr>
		</table>
		<br />
		<hr />
		<div align="right"><em>ข้อความนี้ถูกส่งโดยระบบหลังบ้านของติวเตอร์แลนด์<br /> 
		' . date('jS F Y h:i:s A') .'</em></div>
		</body></html>';
		$this->email->message($msgMail);
		
		$this->email->send();
		
		$this->afterSendMail['success'] = "ได้รับข้อมูลเรียบร้อยแล้วค่ะ ทางโรงเรียนจะติดต่อกลับไปเร็วที่สุดค่ะ";
		$this->index();
	}
	
	
	function edit()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
		$this->load->model('filemanagementmodel');
	    $data['contact_intro'] = $this->filemanagementmodel->getByPostion('contact_intro');
	    $data['contact_01'] = $this->filemanagementmodel->getByPostion('contact_01');
	    $data['contact_02'] = $this->filemanagementmodel->getByPostion('contact_02');
	    $data['contact_03'] = $this->filemanagementmodel->getByPostion('contact_03');
	    $data['contact_04'] = $this->filemanagementmodel->getByPostion('contact_04');
	    $data['contact_05'] = $this->filemanagementmodel->getByPostion('contact_05');
	    
	    $data['contact_map_01'] = $this->filemanagementmodel->getByPostion('contact_map_01');
	    $data['contact_map_02'] = $this->filemanagementmodel->getByPostion('contact_map_02');
	    $data['contact_map_03'] = $this->filemanagementmodel->getByPostion('contact_map_03');
	    $data['contact_map_04'] = $this->filemanagementmodel->getByPostion('contact_map_04');
	    $data['contact_map_05'] = $this->filemanagementmodel->getByPostion('contact_map_05');
	    
		$data['contact_cont'] = $this->filemanagementmodel->getByPostion('contact_cont');
		
		$this->load->view('header');
		$this->load->view('contact_edit', $data);
		$this->load->view('footer');
	}
	
	function upload()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('filemanagementmodel');
	    
	    // save text data 
	    $d = array();
	    $d['position'] = 'contact_intro';
	    $d['longd'] = $this->input->post('txtcontact_intro');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_map_01';
	    $d['detail'] = $this->input->post('txtBranch1');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_map_02';
	    $d['detail'] = $this->input->post('txtBranch2');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_map_03';
	    $d['detail'] = $this->input->post('txtBranch3');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_map_04';
	    $d['detail'] = $this->input->post('txtBranch4');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_map_05';
	    $d['detail'] = $this->input->post('txtBranch5');
		$this->filemanagementmodel->update($d);
		
		$d = array();
	    $d['position'] = 'contact_cont';
	    $d['longd'] = $this->input->post('txtcont');
		$this->filemanagementmodel->update($d);
		
		$msg = array('msg' => '');
	     if(isset($_FILES))
        {
        	$this->load->library('upload');
        	if(@$_FILES['txtFiles1']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png|';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact01';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles1'))
				{
					$msg['msg'] = "ไม่สามารถ upload ไฟล์รูปที่ 1 ได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_01',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	if(@$_FILES['txtFiles2']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact02';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles2')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปที่ 2 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_02',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles3']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact03';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles3')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปที่ 3 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_03',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles4']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact04';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles4')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปที่ 4 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_04',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles5']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact05';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles5')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปที่ 5 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_05',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles6']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact06';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles6')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์สาขาที่ 1 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_map_01',
		               'file_name' => $data['upload_data']['file_name'] 
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles7']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact07';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles7')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์สาขาที่ 2 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_map_02',
		               'file_name' => $data['upload_data']['file_name'] 
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles8']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact08';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles8')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์สาขาที่ 3 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_map_03',
		               'file_name' => $data['upload_data']['file_name'] 
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles9']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact09';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles9')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์สาขาที่ 4 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_map_04',
		               'file_name' => $data['upload_data']['file_name'] 
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if(@$_FILES['txtFiles10']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'contact10';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles10')){
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์สาขาที่ 5 ได้";			
				}
				else {
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'contact_map_05',
		               'file_name' => $data['upload_data']['file_name'] 
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	
        	if($msg['msg'] == '') 
        	{
        		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        	} 
        } else {
        	$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        }
		
		$data['contact_intro'] = $this->filemanagementmodel->getByPostion('contact_intro');
	    $data['contact_01'] = $this->filemanagementmodel->getByPostion('contact_01');
	    $data['contact_02'] = $this->filemanagementmodel->getByPostion('contact_02');
	    $data['contact_03'] = $this->filemanagementmodel->getByPostion('contact_03');
	    $data['contact_04'] = $this->filemanagementmodel->getByPostion('contact_04');
	    $data['contact_05'] = $this->filemanagementmodel->getByPostion('contact_05');
	    
	    $data['contact_map_01'] = $this->filemanagementmodel->getByPostion('contact_map_01');
	    $data['contact_map_02'] = $this->filemanagementmodel->getByPostion('contact_map_02');
	    $data['contact_map_03'] = $this->filemanagementmodel->getByPostion('contact_map_03');
	    $data['contact_map_04'] = $this->filemanagementmodel->getByPostion('contact_map_04');
	    $data['contact_map_05'] = $this->filemanagementmodel->getByPostion('contact_map_05');
		$data['contact_cont'] = $this->filemanagementmodel->getByPostion('contact_cont');
		
		$this->load->view('header');
		$this->load->view('contact_edit', $data);
		$this->load->view('footer', $msg);
	}
}
?>